package com.boyinet.activation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boyinet.activation.entity.SecurityUser;
import com.boyinet.activation.entity.User;
import com.boyinet.activation.mapper.UserMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lengchunyun
 */
@Service(value = "userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws AuthenticationException {
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda().eq(User::getUsername, s);
        User users = userMapper.selectOne(wrapper);
        if (users == null) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        System.out.println(users);
        List<String> auths = new ArrayList<>();
        auths.add("role");
        SecurityUser securityUser = new SecurityUser(users);
        securityUser.setPermissionValueList(auths);
        return securityUser;
    }
}
